﻿// -----------------------------------------------------------------------
//    This file is part of TSGE.
//
//    TSGE is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 2 of the License, or
//    (at your option) any later version.
//
//    TSGE is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with TSGE.  If not, see <http://www.gnu.org/licenses/>.
// -----------------------------------------------------------------------

namespace tsge.Classes
{
    using System;
    using System.Windows.Data;
    using System.Windows.Media;

    /// <summary>
    /// ColorToBrushConverter Class Implementation
    /// 
    ///     Inherits:
    ///         - IValueConverter
    /// </summary>
    public class ColorToBrushConverter : IValueConverter
    {
        /// <summary>
        /// Attempts to convert a Media.Color to a solid brush. 
        /// </summary>
        /// <param name="value">The color to convert to a brush.</param>
        /// <param name="targetType">Media.Brush</param>
        /// <param name="parameter">Not used.</param>
        /// <param name="culture">Culture info of the application.</param>
        /// <returns>New SolidColorBrush</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (!(value is Color))
                throw new InvalidCastException("Value must be of type System.Windows.Media.Color");
            return new SolidColorBrush((Color)value);
        }

        /// <summary>
        /// Attempts to convert a solid brush back to a Media.Color.
        /// </summary>
        /// <param name="value">The brush to convert to a color.</param>
        /// <param name="targetType">Media.Color</param>
        /// <param name="parameter">Not used.</param>
        /// <param name="culture">Culture info of the application.</param>
        /// <returns></returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (!(value is Brush))
                throw new InvalidCastException("Value must be of type System.Windows.Media.Brush");
            return (value as SolidColorBrush).Color;
        }
    }
}
